package com.heima.commonMethods;

/**
 * @Author: Ethan Yankang
 * @Program: 面试必会代码
 * @Date: 2024-08-29 09:02
 **/
public class Join {
    public static void main(String[] args) {
        /*
         * @description 线程A的打印
         * @params
         * @return
         */

        Thread t1 = new Thread(() -> System.out.println("打印A" + Thread.currentThread().getName()), "线程A");
        /*
         * @description 线程B的打印
         * @params
         * @return
         */
        Thread t2 = new Thread(() -> {

            try {
                /*
                 * @description 意思就是，t1调用了join方法，那么会先执行t1线程，在执行t2线程
                 * @params 
                 * @return 
                 */
                
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            for (int i = 0; i < 3; i++) {
                System.out.println("打印B" + Thread.currentThread().getName());

            }
        }, "线程B");
        
         /*
         * @description 线程C的打印
         * @params 
         * @return 
         */
        Thread t3 = new Thread(() -> {
            try {
                /*
                 * @description 意思就是，t2调用了join方法，那么会先执行t2线程，在执行t3线程.注意这里一定会
                 * @params
                 * @return
                 */
                t2.join();

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            for (int i = 0; i < 3; i++) {
                System.err.println("打印C" + Thread.currentThread().getName());
            }

        }, "线程C");

        /*
         * @description 为了说明，我们特意将执行顺序调反
         * @params
         * @return
         */

        t3.start();
        t2.start();
        t1.start();
    }

}
